﻿using HuaCheng.CommonClass;
using System;
using System.Windows.Forms;

namespace HuaCheng.UserControls
{
    public partial class PagerControl : UserControl
    {
        //构造函数
        public PagerControl()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 为当前页面内所有控件设置文本语种
        /// </summary>
        public void SetResourceCulture()
        {
            this.lblCurrentPage.Text = ResourceCulture.GetString("CurrentPage");
            this.lblGong.Text = ResourceCulture.GetString("Gong");
            this.lblRecord.Text = ResourceCulture.GetString("Records");
        }
        #region 分页字段和属性
        private int pageIndex = 1;
        /// <summary>
        /// 当前页数
        /// </summary>
        public virtual int PageIndex
        {
            get { return pageIndex; }
            set { pageIndex = value; }
        }
        private int pageSize = 3;
        /// <summary>
        /// 每页记录数
        /// </summary>
        public virtual int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }
        private int recordCount = 0;
        /// <summary>  
        /// 总记录数  
        /// </summary>  
        public virtual int RecordCount
        {
            get { return recordCount; }
            set { recordCount = value; }
        }
        private int pageCount = 0;
        /// <summary>  
        /// 总页数  
        /// </summary>  
        public int PageCount
        {
            get
            {
                if (pageSize != 0)
                {
                    pageCount = GetPageCount();
                }
                return pageCount;
            }
        }  
        #endregion  
        #region 页码变化时触发事件

        public event EventHandler OnPageChanged;

        #endregion
        #region 分页及相关事件功能实现

        /// <summary>  
        /// 设窗体控件全部可用  
        /// </summary>  
        private void SetFormCtrEnabled()
        {
            btnFirstPage.Enabled = true;
            btnPrePage.Enabled = true;
            btnNextPage.Enabled = true;
            btnLastPage.Enabled = true;
        }
        /// <summary>  
        /// 计算总页数  
        /// </summary>  
        /// <returns></returns>  
        private int GetPageCount()
        {
            if (PageSize == 0)
            {
                return 0;
            }
            int pageCount = RecordCount / PageSize;
            if (RecordCount % PageSize == 0)
            {
                pageCount = RecordCount / PageSize;
            }
            else
            {
                pageCount = RecordCount / PageSize + 1;
            }
            return pageCount;
        }
        /// <summary>  
        /// 用于客户端调用  
        /// </summary>  
        public void DrawControl(int count)
        {
            recordCount = count;
            DrawControl(false);
        }
        /// <summary>  
        /// 根据不同的条件，改变页面控件的呈现状态  
        /// </summary>  
        private void DrawControl(bool callEvent)
        {

            lblCurrNum.Text = PageIndex.ToString();
            lblTotalPage.Text = PageCount.ToString();
            lblTotalNum.Text = RecordCount.ToString();

            if (callEvent && OnPageChanged != null)
            {
                OnPageChanged(this, null);//当前分页数字改变时，触发委托事件  
            }
            SetFormCtrEnabled();
            if (PageCount == 1)//有且仅有一页时  
            {
                btnFirstPage.Enabled = false;
                btnPrePage.Enabled = false;
                btnNextPage.Enabled = false;
                btnLastPage.Enabled = false;
            }
            else if (PageIndex == 1)//当前页为第一页时  
            {
                btnFirstPage.Enabled = false;
                btnPrePage.Enabled = false;
            }
            else if (PageIndex == PageCount)//当前页为最后一页时  
            {
                btnNextPage.Enabled = false;
                btnLastPage.Enabled = false;
            }
        } 
        #endregion

        
        #region 相关控件事件

        //首页按钮
        private void btnFirstPage_Click(object sender, EventArgs e)
        {
            PageIndex = 1;
            DrawControl(true);
        }
        //上一页按钮  
        private void btnPrePage_Click(object sender, EventArgs e)
        {
            PageIndex = Math.Max(1, PageIndex - 1);
            DrawControl(true);  
        }
        //下一页按钮  
        private void btnNextPage_Click(object sender, EventArgs e)
        {
            PageIndex = Math.Min(PageCount, PageIndex + 1);
            DrawControl(true);  
        }
        //尾页按钮  
        private void btnLastPage_Click(object sender, EventArgs e)
        {
            PageIndex = PageCount;
            DrawControl(true);  
        }
        #endregion
    }
}
